package com.dingding.order.processer.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import com.dingding.common.core.base.daox.IBaseDaoX;
import com.dingding.common.core.beans.UserLogin;
import com.dingding.common.core.beans.Userordert;
import com.dingding.common.core.exception.bussiness.BizExpEnum;
import com.dingding.common.core.exception.bussiness.BussinessException;
import com.dingding.common.core.util.DateFormatUtil;
import com.dingding.order.code.order.PublishCarType;
import com.dingding.order.core.common.IUserOrderDao;
import com.dingding.order.core.march.dao.IMarchUserOrderTDao;
import com.dingding.order.core.myorder.dao.IGetMyOrderDao;
import com.dingding.order.dto.history.HistoryOrderDto;
import com.dingding.order.dto.march.underway.UderWayOrderDto;
import com.dingding.order.dto.myorder.MyOrder;
import com.dingding.order.dto.myorder.ReqMyOrderDTO;
import com.dingding.order.dto.pay.paymoney.GetThirdPayAndAccountDto;
import com.dingding.order.processer.IOrderProcesser;

/**
 * @Description:私家车处理
 * @author fengshuonan
 * @date 2016年8月18日 下午5:28:49
 * @version 叮叮搭车
 * @since 2.4
 */
@Component("normalOrderProcesser")
public class NormalOrderProcesser implements IOrderProcesser {

	@Resource(name = "userOrderCommonDao")
	private IUserOrderDao userOrderDao;

	@Resource(name = "marchUserOrderTDao")
	private IMarchUserOrderTDao marchUserOrderTDao;

	@Resource(name = "getMyOrderDao")
	private IGetMyOrderDao getMyOrderDao;

	@Resource(name = "baseDaoX")
	private IBaseDaoX baseDaoX;

	/**
	 * @Description:获取订单状态（私家车的）
	 * @author fengshuonan
	 * @return int (0:无此订单 1:待抢单 2:待支付 3:已支付 4:已完成 5:已取消 )
	 * @since 2.4
	 */
	@Override
	public int getOrderStatus(String orderId) {
		Integer status = this.userOrderDao.getUserOrderStatus(orderId);
		if (status == null) {
			return 0;
		} else {
			return status;
		}
	}

	@Override
	public GetThirdPayAndAccountDto getThirdPayAndAccountPay(BigDecimal accountRemain, String orderId) {
		Userordert userordert = this.userOrderDao.getById(orderId);
		BigDecimal totalMoney = userordert.getTotalMoney(); // 订单中的里程费用
		if (totalMoney == null) {
			return null;
		}
		BigDecimal couponMoney = userordert.getCouponMoney(); // 订单中的优惠费用
		BigDecimal orderMoney = new BigDecimal(0); // 订单应该支付的费用
		if (couponMoney != null) {
			orderMoney = userordert.getTotalMoney().add(new BigDecimal(2)).subtract(userordert.getCouponMoney());// 订单金额等于
		} else {
			orderMoney = userordert.getTotalMoney().add(new BigDecimal(2));// 订单金额等于
		}
		GetThirdPayAndAccountDto dto = new GetThirdPayAndAccountDto();
		if (accountRemain.compareTo(orderMoney) < 0) {
			dto.setAccountPayMoney(accountRemain);
			dto.setThirdPayMoney(orderMoney.subtract(accountRemain));
		} else {
			dto.setAccountPayMoney(orderMoney);
			dto.setThirdPayMoney(new BigDecimal(0));
		}
		return dto;
	}

	@Override
	public List<UderWayOrderDto> getUnderWayOrders(String userId, Integer clientType) {
		return this.marchUserOrderTDao.getShunfengUnderWayOrder(userId);
	}

	@Override
	public List<HistoryOrderDto> getHistoryOrders(String userId, int orderStatus, int currentPage, int pageSize,
			Integer userType) {
		if (orderStatus == 5) {// 如果是已完成订单
			orderStatus = 4;
		} else if (orderStatus == 6) {// 如果是已取消
			orderStatus = 5;// 已取消
		}
		ReqMyOrderDTO reqMyOrderDTO = new ReqMyOrderDTO();
		reqMyOrderDTO.setOrderStatus(orderStatus);
		reqMyOrderDTO.setPageIndex(currentPage);
		reqMyOrderDTO.setPageSize(pageSize);
		reqMyOrderDTO.setUserId(userId);
		List<MyOrder> myOrders = this.getMyOrderDao.getMyOrderList(reqMyOrderDTO);
		if (myOrders == null) {
			return new ArrayList<HistoryOrderDto>();
		}
		List<HistoryOrderDto> historyOrderDtos = new ArrayList<HistoryOrderDto>();
		for (MyOrder order : myOrders) {
			HistoryOrderDto historyOrderDto = new HistoryOrderDto();
			BeanUtils.copyProperties(order, historyOrderDto);
			historyOrderDto.setSetOutTime(DateFormatUtil.parseMMddWWHHSS(order.getSetOutTime()));

			// 取消原因 1:自己取消 2:对方取消取消 3:自动取消
			historyOrderDto.setCancelType(historyOrderDto.getCancelRerson());

			// 发布订单类型
			historyOrderDto.setPublishType(order.getRegistrationType());

			// 遍历所有顺风车订单记录到集合并返回
			if (order.getRegistrationType() == PublishCarType.SHUN_FENG_CHE.getCode()) {
				historyOrderDto.setPublishType(PublishCarType.SHUN_FENG_CHE.getCode());
				historyOrderDtos.add(historyOrderDto);
			}
		}
		return historyOrderDtos;
	}

	@Override
	public String getCalledUserTell(String orderId, String userId) {

		Userordert ordert = this.baseDaoX.getById(orderId, Userordert.class);

		if (ordert == null) {
			throw new BussinessException(BizExpEnum.NO_THIS_ORDER);
		}

		String calledUserId = null;

		// 如果获取被叫电话号码时，用户id为下单的用户id，则获取被叫电话号码为抢单用户id的电话号码
		if (userId.equals(ordert.getPlaceOrderUserId())) {
			calledUserId = ordert.getGrabOrderUserId();
		} else {
			calledUserId = ordert.getPlaceOrderUserId();
		}

		// 通过用户id获取用户的手机号
		String telephone = this.userOrderDao.getUserTelephone(calledUserId);

		return telephone;
	}

	@Override
	public String getUserTellByUserId(String userId) {

		UserLogin userlogin = this.baseDaoX.getById(userId, UserLogin.class);

		if (userlogin == null) {
			throw new BussinessException(BizExpEnum.NO_THIS_USER);
		}

		return userlogin.getUserName();
	}

	@Override
	public String getCallUserTell(String orderId, String userId) {
		
		return this.getUserTellByUserId(userId);
	}

	@Override
	public List<HistoryOrderDto> completeAndCancelOrder(String userId, int currentPage, int pageSize,
			Integer userType) {
		// TODO Auto-generated method stub
		return null;
	}

}
